home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7756 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.1 KB  |  124 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!eskimo!scs
  3. From: scs@eskimo.com (Steve Summit)
  4. Subject: Re: Using char array in functions
  5. X-Nntp-Posting-Host: eskimo.com
  6. Message-ID: <DnI1KG.EtF@eskimo.com>
  7. Sender: news@eskimo.com (News User Id)
  8. Organization: schmorganization
  9. References: <20FEB199609155775@sundog.caltech.edu> <4gqk5m$jno@aphex.direct.ca>
  10. Date: Wed, 28 Feb 1996 18:48:16 GMT
  11.  
  12. In article <4gqk5m$jno@aphex.direct.ca>, etoivane@direct.ca (Ed Toivanen)
  13. writes:
  14. > In article <20FEB199609155775@sundog.caltech.edu>, 
  15. > taylor@sundog.caltech.edu says...
  16. >> I've searched the faq's and postings, but still am looking for
  17. >> a clear explanation on how to use arrays of char strings in
  18. >> functions, especially when I want to have the function modify
  19. >> the array.  I find I can't just pass the
  20. >> name of the string as a pointer as I do for single strings.
  21. >  ^^^^
  22. > As I just learned from my midterm(the hard way, in other
  23. > words!) you have to pass the address of the name of the array
  24. > if you expext to modify more than a copy of the thing.
  25.  
  26. Huh?  Either taylor@sundog, or Ed Toivanen, or I, or some
  27. combination of us is/am/are confused.  (I'm so confused I can't
  28. even figure out what verb to use!)
  29.  
  30. C doesn't use pass-by-name, so let's not use this word "name."
  31. C does use pass-by-value, which means that functions receive
  32. copies of their actual arguments, which means that functions
  33. cannot modify their arguments and have those modifications
  34. reflected in values in the caller, with the possible exception of
  35. arrays.  Superficially, it looks as if a function which modifies
  36. an array which has been passed to it *does* modify the array in
  37. the caller, and it's not a bad rule to remember that arrays are
  38. an exception to the rule that functions operate on local copies
  39. of their arguments (or, stated another way, that arrays are an
  40. exception to the rule that functions can't modify the values in
  41. the caller which it passed as arguments).
  42.  
  43. The word "superficially" in the previous paragraph appears there
  44. because it's not strictly true that a function can modify an
  45. array which was passed to it.  It can indeed modify the array in
  46. the caller, but what was passed to the function was not the
  47. array, but rather a pointer to the array's first element.
  48. When you look at it that way, there's no exception to the
  49. call-by-value rule: a function receives a copy of its pointer
  50. argument, and it could only modify that copy of the pointer, but
  51. it can certainly *use* that pointer (without even modifying it)
  52. to modify what it *points to*, namely something in the caller.
  53.  
  54. A canonical example is the function getline:
  55.  
  56.     char line[100];
  57.     int len = getline(line, 100);
  58.  
  59. This hypothetical (or see K&R for concrete examples) getline
  60. function returns two things: the length of the line it reads (as
  61. its formal return value), and the contents of the line it reads,
  62. which it "returns" by writing it into what appears to be its
  63. array argument.  (Another way of thinking about the situation is
  64. that getline's first argument tells getline where to write the
  65. line.)
  66.  
  67. It is perfectly legal, quite possible, and most common for a
  68. function to modify an array which seems to have been passed to it
  69. (usually by mentioning the array's name in the function call
  70. argument list).  The only time this wouldn't work would be if the
  71. array were not modifiable, either because it was declared const
  72. or because it is a string literal.  The call to getline above
  73. would work, but these three might not:
  74.  
  75.     const char constline[100];
  76.     len = getline(constline, 100);
  77.  
  78.     char *p = "Hello, world!";
  79.     getline(p, 100);
  80.  
  81.     getline("Hello, world!", 100);
  82.  
  83. These calls would not work because, again, the arrays are not
  84. modifiable.  It is hard to imagine a situation in which a
  85. function was unable to modify what appeared to be an array
  86. parameter for the reason that it was operating on a copy of the
  87. array.  (The only way I can imagine this failure mode is if the
  88. array is wrapped up in a struct, and in that case the function
  89. would no longer appear to be receiving an array parameter; it
  90. would be receiving a struct parameter.)
  91.  
  92. Finally, as we've just been discussing in another thread, you
  93. don't need (and, in fact, rarely want) an explicit "address of"
  94. operator & when you're passing a pointer down to a function so
  95. that the function can modify one of your arrays.  When you write
  96.  
  97.     getline(line, 100)
  98.  
  99. it's as if you had written
  100.  
  101.     getline(&line[0], 100)
  102.  
  103. and it's that implicit & which generates the pointer that getline
  104. actually receives (or, speaking more strictly still, that getline
  105. receives a copy of).  If you were to write
  106.  
  107.     getline(&line, 100)
  108.  
  109. it would be incorrect, because getline would receive a pointer-
  110. to-array-of-char instead of the pointer-to-char it expects.
  111. (Since, on most machines, these two types are likely to have the
  112. same sizes and representations, and for the same array to have
  113. the same values, the incorrect call involving &line is likely to
  114. work, but for the wrong reason.)
  115.  
  116.                     Steve Summit
  117.                     scs@eskimo.com
  118. -- 
  119. The Communications Decency Act within the Telecommunications Act
  120. of 1996 (U.S.) is an annoying, threatening, abusive, indecent,
  121. and obscene piece of legislation which attempts to ban annoying,
  122. threatening, abusive, indecent, or obscene communication.
  123.